package cn.com.hhrcw.base.position.web;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.industry.mapstruct.RecruitmentPositionConvert;
import cn.com.hhrcw.industry.vo.RecruitmentPositionVO;
import cn.com.hhrcw.params.CommonParams;
import cn.com.hhrcw.position.entity.RecruitmentPosition;
import cn.com.hhrcw.position.service.IRecruitmentPositionService;
import cn.com.hhrcw.utils.SubjectUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
/**
 * 职位类别管理
 *
 * @author yinyinfeng
 * @date 2020年11月07日10:35:10
 */
@RestController
@RequestMapping("/base/position")
@Validated
@Slf4j
public class PositionController {

    @Autowired
    private IRecruitmentPositionService recruitmentPositionService;
    @Autowired
    private RecruitmentPositionConvert recruitmentPositionConvert;
    /**
     * 列表查询
     *
     * @param position 职位名称
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "列表查询", notes = "列表查询")
    @PermissionData
    public Result<List<RecruitmentPositionVO>> list(String position) {
    	List<RecruitmentPosition> positions = recruitmentPositionService.list();
        //一级职类
        List<RecruitmentPositionVO> collect = positions.stream().filter(e -> StrUtil.isBlank(e.getPCode())).map(recruitmentPositionConvert::toVO).collect(Collectors.toList());
        for (RecruitmentPositionVO vo : collect) {
            //二级职类
            List<RecruitmentPositionVO> children = positions.stream().filter(e -> StrUtil.equals(e.getPCode(), vo.getCode())).map(recruitmentPositionConvert::toVO).collect(Collectors.toList());
            vo.setChildren(children);
            for (RecruitmentPositionVO svo : children) {
                //三级职位
                List<RecruitmentPositionVO> last = positions.stream().filter(e -> StrUtil.equals(e.getPCode(), svo.getCode())).map(recruitmentPositionConvert::toVO).collect(Collectors.toList());
                svo.setChildren(last);
            }
        }
        return Result.ok(collect);
    }

    /**
     * 岗位新增
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog("岗位-添加")
    @ApiOperation(value = "添加", notes = "添加")
    public Result<?> add(@RequestBody @Valid RecruitmentPosition position) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        position.setId(IdUtil.fastSimpleUUID());
        position.setCode(IdUtil.fastSimpleUUID());
        position.setCreateBy(sysUser.getId());
        position.setCreateTime(DateUtil.date());
        boolean saved = recruitmentPositionService.save(position);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
     * 岗位编辑
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/edit")
    @AutoLog("岗位-编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    public Result<?> edit(@RequestBody @Valid RecruitmentPosition params) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        params.setUpdateBy(sysUser.getId());
        params.setUpdateTime(DateUtil.date());

        boolean updated = recruitmentPositionService.updateById(params);
        return updated ? Result.ok() : Result.error("编辑失败");
    }

    /**
     * 批量删除
     *
     * @param params 数据id
     * @return
     */
    @PostMapping(value = "/delete")
    @AutoLog("岗位-批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result<?> delete(@RequestBody CommonParams params) {
        RecruitmentPosition one = this.recruitmentPositionService.lambdaQuery()
            .eq(RecruitmentPosition::getId, params.getId())
            .one();
        Assert.notNull(one, "删除失败");
        boolean deleted = recruitmentPositionService.removeById(params.getId());
        return deleted ? Result.ok() : Result.error("删除失败");
    }
}
